package com.halflife.framework.generator.template.mapper;

import com.halflife.framework.generator.GenerateUtil;
import com.halflife.framework.generator.annotation.QueryAnn;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * Created by zhaoyf on 2018/6/18.
 */
public class WA_SqlAnalysis extends BaseAnalysis {
    public WA_SqlAnalysis(Class clazz) {
        super(clazz);
    }
    private int index=0;

    @Override
    protected void setPrefix() {
        this.xmlStr.append( "<sql id=\"Base_Column_List\">\n");

    }
    protected void fieldAnaysis1() {
        this.joinStrs = new StringBuilder();
        final int[] len = {0};
        QueryAnn classAnn =(QueryAnn) this.clazz.getAnnotation(QueryAnn.class);
        this.joinStrs.append("`").append(classAnn.table()).append("` ");
        this.joinStrs.append(prefixs[objs]);
        for (Field field : fields) {

            if (field.getType().getPackage().getName().indexOf("com.halflife") == -1){
                this.setValue(field,prefixs[objs],null);
            }else{
                QueryAnn ann = field.getAnnotation(QueryAnn.class);
//                if (StringUtils.isNotBlank(ann.join())) {
//                    this.setJoinStr(this.clazz,field);
//                    this.fillField(field);
//                }

            }
            this.index++;
        }
    }



    private void setValue(Field field,String prefix,String resultPrefix) {
        String typeName = field.getType().getSimpleName().toLowerCase();
        String value = null;
        value = GenerateUtil.getDbType(typeName).toUpperCase();
        if (this.index > 0) {
            xmlStr.append(", ");
        }
        xmlStr.append(prefix).append(".");
        xmlStr.append(field.getName());
        if (StringUtils.isNotBlank(resultPrefix)) {
            xmlStr.append(" as ").append(resultPrefix).append("_").append(field.getName());
        }

    }
    private Map joinMap;

    private void fillField(Field field) {
        objs++;
        //todo
        Class c = field.getType();
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            QueryAnn ann = f.getAnnotation(QueryAnn.class);
            if (ann!=null&&ann.refered()) {
//                if (f.getType().getPackage().getName().indexOf("com.halflife") == -1){
//                    this.setValue(f,prefixs[objs],c.getSimpleName());
//                }
//                if (ann != null && StringUtils.isNotBlank(ann.join())) {
//                    this.joinStrs.append("\n\t ").append(ann.join()).append(" ");
//                    this.fillField(f);
//                }
            }

        }
        System.out.println(this.joinStrs.toString());
    }

    @Override
    protected String getValue1(CustomField customField) {
        return "";
    }



    ;

    protected void setSuffix() {

        this.xmlStr.append( "\n</sql>");
    }

    ;
}
